﻿using Biz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;
using BusinessLayer.Dal;
using PagedList;
using PagedList.Mvc;
namespace Web.Controllers
{
    public class ProductController : Controller
    {
        private DatabaseContext db = new DatabaseContext();
        private Product product = new Product();
        private Manufacturer manufacturer = new Manufacturer();
        private Category cat = new Category();
        public ActionResult Index()
        {
            
            return View();
        }

        public ActionResult Search(int? page)
        {
            string name = Session["search_name"]!=null?Session["search_name"].ToString():"";
            int from = Session["search_from"] != null ? int.Parse(Session["search_from"].ToString()) : 0;
            int to = Session["search_to"] != null ? int.Parse(Session["search_to"].ToString()) : product.MaxPrice();
            int categoryid = Session["search_category"] != null ? int.Parse(Session["search_category"].ToString()) : 0;
            int manufacturerid = Session["search_manufacturer"] != null ? int.Parse(Session["search_manufacturer"].ToString()) : 0;
            
            ViewBag.Categories = new SelectList(cat.GetAll(), "Id", "Name",categoryid);
            ViewBag.Manufacturers = new SelectList(manufacturer.GetAll(), "Id", "Name",manufacturerid);
            var pageNumber = page??1;

            List<Product> products = product.SearchByCondition(name, from, to, categoryid,manufacturerid);
            ViewBag.Count = products.Count;
            ViewBag.Products = products.ToPagedList(pageNumber, 6);
            return View();
        }

        [HttpPost]
        public ActionResult Search(FormCollection collection)
        {
            Session["search_name"] = collection["name"];
            int temp;
            Session["search_from"] = int.TryParse(collection["from"],out temp)?collection["from"]:"0";
            Session["search_to"] = int.TryParse(collection["to"], out temp) ? collection["to"] : product.MaxPrice().ToString();
            Session["search_category"] = int.TryParse(collection["Categories"], out temp) ? collection["Categories"] : "0";
            Session["search_manufacturer"] = int.TryParse(collection["Manufacturers"], out temp) ? collection["Manufacturers"] : "0";
            return RedirectToAction("Search");
        }
        public ActionResult Refresh()
        {
            Session["search_name"] = null;
            Session["search_from"] = null;
            Session["search_to"] = null;
            Session["search_category"] = null;
            Session["search_manufacturer"] = null;
            return RedirectToAction("Search");
        }
        public ActionResult Detail(int id)
        {
            ViewBag.Product = product.GetProductById(id);
            ViewBag.Category = ViewBag.Product.Category;
            ViewBag.Comments = ViewBag.Product.Comments;
            return View();
        }
        
        public ActionResult Category(int id,int? page)
        {
            ViewBag.Category = cat.GetById(id);
            ViewBag.Categories = cat.GetAll();
            var pageNumber = page ?? 1;
            ViewBag.Products = product.GetByCategory(cat.GetById(id)).ToPagedList(pageNumber,9);
            return View();
        }

        [ChildActionOnly]
        public ActionResult __CategoriesMenu()
        {
            ViewBag.Categories = cat.GetAll();
            return PartialView();
        }
    }
}
